﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace ZhimeiSoft.Framework.Client.Control.DateTimeGrid
{
    public delegate void EventClickCurrDay(object iDateTimeView);

    public delegate void EventDBClickCurrDay(object iDateTimeView);

    public delegate void EventCurrMonth(DateTime currMonth);

    public partial class MonthDateTimeForDay : UserControl, IDateTimeView
    {
        /// <summary>
        /// 当前的时间
        /// </summary>
        private DateTime _dateTime = new DateTime();
        /// <summary>
        /// 当前控件是否有效
        /// </summary>
        private bool ControlEffective { get; set; }
        /// <summary>
        /// 显示字段
        /// </summary>
        private string displayFieldName = string.Empty;
        /// <summary>
        /// 值字段
        /// </summary>
        private string valueFieldName = string.Empty;
        /// <summary>
        /// 保存使用值
        /// </summary>
        private ArrayList valueList = new ArrayList();
        /// <summary>
        /// 当前保存的数据集
        /// </summary>
        private List<DataRow> dataRowList = new List<DataRow>();
        /// <summary>
        /// 当前控件单击
        /// </summary>
        public EventClickCurrDay OnClickCurrDay = null;
        /// <summary>
        /// 当前控件双击
        /// </summary>
        public EventDBClickCurrDay OnDBClickCurrDay = null;

        public String ShowToolTipFieldName = "SMEMO";

        public MonthDateTimeForDay()
        {
            InitializeComponent();
        }


        #region IDateTimeView 成员
        /// <summary>
        ///获取当前控件的时间
        /// </summary>
        /// <returns></returns>
        public DateTime GetDateTime()
        {
            return _dateTime;
        }
        /// <summary>
        /// 设置当前控件的时间
        /// </summary>
        /// <param name="dateTime"></param>
        public void SetDateTime(DateTime dateTime)
        {
            _dateTime = new DateTime(dateTime.Ticks);
            string textMonth = string.Concat(dateTime.Month, '月');
            if(dateTime.Day != 1)
                textMonth = "";
            labelMonth.BeginUpdate();
            labelChineseMonth.BeginUpdate();
            try
            {
                labelMonth.Text = string.Concat(textMonth, dateTime.Day, '日');
                labelChineseMonth.Text = ChineseDateTime.GetChineseDateTime(dateTime);
            }
            finally
            {
                labelMonth.EndUpdate();
                labelChineseMonth.EndUpdate();
            }
        }
        /// <summary>
        /// 设置当前控件的颜色
        /// </summary>
        /// <param name="bEffective"></param>
        public void SetEffectiveColor(bool bEffective)
        {
            if (bEffective)
            {
                listBox.BackColor = System.Drawing.SystemColors.Window;
            }
            else
            {
                listBox.BackColor = System.Drawing.SystemColors.InactiveCaption;
            }
            ControlEffective = bEffective;
        }
        /// <summary>
        /// 返回当前所选择项目的索引
        /// </summary>
        /// <returns></returns>
        public int GetItemID()
        {
            return listBox.SelectedIndex;
        }
        /// <summary>
        /// 删除一个项目
        /// </summary>
        /// <param name="indexID"></param>
        public void DelItem(int indexID)
        {
            if (indexID < 0)
                return;

            listBox.Items.RemoveAt(indexID);
            valueList.RemoveAt(indexID);
            dataRowList.RemoveAt(indexID);
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="dtInit"></param>
        public void InitDate(DateTime dtInit)
        {
            labelMonth.BeginUpdate();
            labelChineseMonth.BeginUpdate();
            try
            {
                labelMonth.Text = "日期";
                labelChineseMonth.Text = "农历";
            }
            finally
            {
                labelMonth.EndUpdate();
                labelChineseMonth.EndUpdate();
            }
        }
        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="dsData">绑定</param>
        public void SetDataBind(DataSet dsData, String displayFieldKey, string valueFieldKey, string timeKey)
        {
            DataTable dsSource = dsData.Tables[0].Copy();
            DataRow[] drRow = dsSource.Select("", "", DataViewRowState.CurrentRows);
            foreach (DataRow dr in drRow)
            {
                listBox.Items.Add(dr[displayFieldKey].ToString());
                valueList.Add(dr[valueFieldKey]);
                dataRowList.Add(dr);
            }
        }
        /// <summary>
        /// 获取当前数据
        /// </summary>
        /// <returns></returns>
        public DataRow GetCurrDataRow(){
            if(listBox.SelectedIndex == -1){
                return null;
            }
            return dataRowList[listBox.SelectedIndex];
        }
        /// <summary>
        /// 获取当前数据
        /// </summary>
        /// <returns></returns>
        public object GetCurrValue(){
            if(listBox.SelectedIndex == -1){
                return null;
            }
            if (listBox.SelectedIndex >= valueList.Count)
                return null;

            return valueList[listBox.SelectedIndex];
        }

        public void ClearBindData()
        {
            listBox.Items.Clear();
            valueList.Clear();
            dataRowList.Clear();
        }
        /// <summary>
        /// 取消当前选择项
        /// </summary>
        public void UnSelectItem()
        {
            listBox.SelectedIndex = -1;
        }

        #endregion

        private void labelMonth_Click(object sender, EventArgs e)
        {
            if (OnClickCurrDay != null)
            {
                OnClickCurrDay(this);
            }
            if (listBox.SelectedIndex == -1)
            {
                return;
            }
            if (dataRowList[listBox.SelectedIndex].Table.Columns.IndexOf(ShowToolTipFieldName) > 0)
            {
                toolTipController1.ShowHint(dataRowList[listBox.SelectedIndex][ShowToolTipFieldName].ToString());
            }
        }

        private void listBox_DoubleClick(object sender, EventArgs e)
        {
            if (OnDBClickCurrDay != null)
            {
                OnDBClickCurrDay(this);
            }
        }

        private void labelMonth_Click_1(object sender, EventArgs e)
        {
            StringBuilder sbText = new StringBuilder();
            for (int i = 0; i < listBox.Items.Count; i++)
            {
                sbText.AppendLine(listBox.Items[i].ToString());
            }
            if (sbText.Length == 0)
            {
                toolTipController2.ShowHint("");
            }
            else
            {
                DevExpress.Utils.ToolTipControlInfo a = new DevExpress.Utils.ToolTipControlInfo();
                a.Text = sbText.ToString();
                toolTipController2.ShowHint(a);
            }
        }
    }
}
